//
//  main.m
//  HomeWork-weekend
//
//  Created by 岁变 on 16/7/8.
//  Copyright © 2016年 岁变. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[]) {
#if 0
#pragma mark - 第一题
   // 1.创建1个包含5个字符串元素的数组；
   // 2.用for循环倒序输出所有元素；
   // 3.用for...in完成所有字符串元素的拼接(生成一个新的字符串)，并输出拼接好的字符串。
    
    NSArray *arr = @[@"小明", @"小王", @"小刘", @"小马", @"小随"];
    for (NSInteger i = arr.count - 1; i >= 0; i--) {
        NSString *str = [arr objectAtIndex:i];
        NSLog(@"%@", str);
    }
    NSInteger a = 0;
    NSMutableString *newStr = [NSMutableString string];
    for (NSString *str in arr) {
        if (a == 0) {
            newStr = str.mutableCopy;
        } else {
            [newStr appendString:str];
        }
        a++;
    }
    NSLog(@"%@", newStr);
#endif
#if 0
#pragma mark - 第二题
    //1.创建1个包含5个person对象的数组(姓名用英文)；
    //2.用for循环找到年龄最大的Person对象；
    //3.用for...in遍历数组，把大于某个年龄的Person对象的姓名后面拼接上字符串“-brother”。
    Person *per1 = [Person personWithName:@"zhangyi" age:20];
    Person *per2 = [Person personWithName:@"zhanger" age:12];
    Person *per3 = [Person personWithName:@"zhangsan" age:22];
    Person *per4 = [Person personWithName:@"zhagnsi" age:10];
    Person *per5 = [Person personWithName:@"zhangwu" age:24];
    NSArray *arr = @[per1, per2, per3, per4, per5];
//    NSInteger temp = 0;
//    NSInteger index = 0;
//    for (NSInteger i = 0; i < arr.count; i++) {
//        Person *per = [arr objectAtIndex:i];
//        if (per.age > temp) {
//            temp = per.age;
//            index = i;
//        }
//    }
//    NSLog(@"%@", [arr objectAtIndex:index]);
//    
//    for (Person *per in arr) {
//        if (per.age >= 23) {
//            per.name = [per.name stringByAppendingString:@"-brother"];
//        }
//        NSLog(@"%@", per);
//    }
    
#pragma mark - 第三题
//    1.使用第二题目中生成的数组；
//    2.使用sortUsingSelector: 分别按年龄，姓名，性别进行排序，输出排序结果；
//    3.使用NSSortDescriptor，按姓名进行排序。
    NSArray *arr1 = [arr sortedArrayUsingSelector:@selector(asSortedByAge:)];
    for (Person *per in arr1) {
        NSLog(@"%@", per);
    }
    
    NSArray *arr2 = [arr sortedArrayUsingSelector:@selector(asSortedByName:)];
    for (Person *per in arr2) {
        NSLog(@"%@", per);
    }
    
    NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    NSArray *arr3 = [arr sortedArrayUsingDescriptors:@[descriptor]];
    for (Person *per in arr3) {
        NSLog(@"%@", per);
    }
    
#endif
#if 0
#pragma mark - 第四题
//    处理省市区文件中的area.txt
//    将省市区进行分割处理, 省里包含市, 市里包含区
//    1. 先用数组字典表示
//    2. 将数组字典的处理方式转换成数组模型(Class)
    
    NSString *sourceData = [NSString stringWithContentsOfFile:@"/Users/Aim/Desktop/Homework-05/area.txt" encoding:NSUTF8StringEncoding error:nil];

    NSArray *arr = [sourceData componentsSeparatedByString:@"\n"];
    
    NSMutableArray *proArr = [NSMutableArray array];
    for (NSString *str in arr) {
        //如果是省名
        if (![str hasPrefix:@" "]) {
            NSMutableDictionary *proDic = [NSMutableDictionary dictionary];
            [proDic setObject:str forKey:@"省名"];
            NSMutableArray *cityArr = [NSMutableArray array];
            [proDic setObject:cityArr forKey:@"城市"];
            [proArr addObject:proDic];
        }
        //如果是城市名
        if ([str hasPrefix:@"  "] && ![str hasPrefix:@"   "]) {
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
            [cityDic setObject:str forKey:@"城市名"];
            NSMutableArray *areArr = [NSMutableArray array];
            [cityDic setObject:areArr forKey:@"区"];
            [[[proArr lastObject] objectForKey:@"城市"] addObject:cityDic];
        }
        //如果是区名
        else {
            [[[[[proArr lastObject] objectForKey:@"城市"] lastObject] objectForKey:@"区"] addObject:str];
        }
    }
    
    for (NSDictionary *proDic in proArr) {
        NSLog(@"省名:%@", [proDic objectForKey:@"省名"]);
        NSArray *cityArr = [proDic objectForKey:@"城市"];
        for (NSDictionary *cityDic in cityArr) {
            NSLog(@"城市:%@", [cityDic objectForKey:@"城市名"]);
            NSArray *areArr = [cityDic objectForKey:@"区"];
            for (NSString *str in areArr) {
                NSLog(@"区:%@", str);
            }
        }
    }
#endif
#if 1
#pragma mark - 第五题
    NSString *source = [NSString stringWithContentsOfFile:@"/Users/Aim/Desktop/Homework-05/Location1.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *sourceArr = [source componentsSeparatedByString:@","];
    NSMutableArray *proArr = [NSMutableArray array];
    for (NSString *sourceData in sourceArr) {
        NSArray *arr = [sourceData componentsSeparatedByString:@"|"];
        if ([arr[1] hasSuffix:@"0000"]) {
            NSMutableDictionary *proDic = [NSMutableDictionary dictionary];
            [proDic setObject:sourceData forKey:@"proName"];
            NSMutableArray *cityArr = [NSMutableArray array];
            [proDic setObject:cityArr forKey:@"city"];
            [proArr addObject:proDic];
        }
        else if ([arr[1] hasSuffix:@"00"] && ![arr[1] hasSuffix:@"0000"]) {
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
            [cityDic setObject:sourceData forKey:@"cityName"];
            NSMutableArray *areArr = [NSMutableArray array];
            [cityDic setObject:areArr forKey:@"area"];
            for (NSDictionary *dic in proArr) {
                NSString *str = [dic objectForKey:@"proName"];
                NSArray *proNameArr = [str componentsSeparatedByString:@"|"];
                if ([proNameArr[1] hasPrefix:[arr[1] substringToIndex:1]]) {
                    NSMutableArray *cityArr = [dic objectForKey:@"city"];
                    [cityArr addObject:cityDic];
                }
            }
    }
    else {
            NSString *preStr = [arr[1] substringToIndex:1];
            NSString *fourthStr = [arr[1] substringWithRange:NSMakeRange(3, 1)];
//        NSLog(@"%@", fourthStr);
            for (NSDictionary *dic in proArr) {
                NSString *str = [dic objectForKey:@"proName"];
                NSArray *proNameArr = [str componentsSeparatedByString:@"|"];
                if ([proNameArr[1] hasPrefix:preStr]) {
                    NSMutableArray *cityArr = [dic objectForKey:@"city"];
                    for (NSDictionary *dic in cityArr) {
                        NSString *str = [dic objectForKey:@"cityName"];
                        NSArray *cityNameArr = [str componentsSeparatedByString:@"|"];
                        if ([[cityNameArr[1] substringWithRange:NSMakeRange(3, 1)] isEqualToString:fourthStr]) {
                            [[dic objectForKey:@"area"] addObject:sourceData];
                        }
                    }
                }
            }
        }
    }
//    for (NSDictionary *proDic in proArr) {
//        NSLog(@"省名:%@", [proDic objectForKey:@"proName"]);
//        NSArray *cityArr = [proDic objectForKey:@"city"];
//        for (NSDictionary *cityDic in cityArr) {
//            NSLog(@"城市:%@", [cityDic objectForKey:@"cityName"]);
//            NSArray *areArr = [cityDic objectForKey:@"area"];
//            for (NSString *str in areArr) {
//                NSLog(@"区:%@", str);
//            }
//        }
//    }
    NSLog(@"%@", proArr);

#endif
    return 0;
}



